using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;

namespace FaceCat
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public MainPage()
        {
            this.InitializeComponent();
            onLoad();
        }

        /// <summary>
        /// 是否双击
        /// </summary>
        private bool m_isDoubleClick;

        /// <summary>
        /// 公共方法库
        /// </summary>
        public FCNative m_native;

        /// <summary>
        /// 设备对接
        /// </summary>
        public UWPHost m_host;

        /// <summary>
        /// 配置解析
        /// </summary>
        public FCUIXml m_xml;

        /// <summary>
        /// 重绘事件
        /// </summary>
        /// <param name="sender">调用者</param>
        /// <param name="args">参数</param>
        public void canvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
        {
            if (m_host.m_canvas == null)
            {
                m_host.m_canvas = sender;
                sender.PointerPressed += Sender_PointerPressed;
                sender.PointerReleased += Sender_PointerReleased;
                sender.PointerMoved += Sender_PointerMoved;
                sender.PointerWheelChanged += Sender_PointerWheelChanged;
            }
            int width = (int)sender.Size.Width, height = (int)sender.Size.Height;
            FCSize oldSize = m_native.getSize();
            if (width > 0 && height > 0)
            {
                if (oldSize.cx != width || oldSize.cy != height)
                {
                    m_native.setSize(new FCSize(width, height));
                    m_native.update();
                }
            }
            UWPPaint aPaint = m_native.getPaint() as UWPPaint;
            aPaint.m_args = args;
            aPaint.m_canvas = sender;
            FCRect clipRect = new FCRect(0, 0, m_native.getSize().cx, m_native.getSize().cy);
            aPaint.beginPaint(IntPtr.Zero, clipRect, clipRect);
            m_native.onPaint(clipRect);
            aPaint.endPaint();
        }

        /// <summary>
        /// 鼠标滚轮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerWheelChanged(object sender, PointerRoutedEventArgs e)
        {
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_delta = e.GetCurrentPoint(sender as UIElement).Properties.MouseWheelDelta;
                m_native.onMouseWheel(newTouchInfo);
            }
        }

        /// <summary>
        /// 鼠标松开事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerReleased(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(sender as UIElement).Position;
            m_host.m_touchPoint = new FCPoint((int)point.X, (int)point.Y);
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = m_isDoubleClick ? 2 : 1;
                m_native.onMouseUp(newTouchInfo);
            }
        }

        /// <summary>
        /// 鼠标移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(sender as UIElement).Position;
            m_host.m_touchPoint = new FCPoint((int)point.X, (int)point.Y);
            if (m_host.allowOperate())
            {
                if (m_native.m_touchDownView != null)
                {
                    FCTouchInfo newTouchInfo = new FCTouchInfo();
                    newTouchInfo.m_firstTouch = true;
                    newTouchInfo.m_buttons = 1;
                    newTouchInfo.m_clicks = 1;
                    m_native.onMouseMove(newTouchInfo);
                }
                else
                {
                    FCTouchInfo newTouchInfo = new FCTouchInfo();
                    m_native.onMouseMove(newTouchInfo);
                }
            }
        }

        /// <summary>
        /// 上次点击时间
        /// </summary>
        public DateTime m_lastMouseDownTime = DateTime.Now;

        /// <summary>
        /// 鼠标按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sender_PointerPressed(object sender, PointerRoutedEventArgs e)
        {
            Point point = e.GetCurrentPoint(sender as UIElement).Position;
            m_host.m_touchPoint = new FCPoint((int)point.X, (int)point.Y);
            DateTime nowTime = DateTime.Now;
            long ticks = (DateTime.Now - m_lastMouseDownTime).Ticks;
            m_isDoubleClick = ticks < 10000 * 200;
            m_lastMouseDownTime = nowTime;
            if (m_host.allowOperate())
            {
                FCTouchInfo newTouchInfo = new FCTouchInfo();
                newTouchInfo.m_firstTouch = true;
                newTouchInfo.m_buttons = 1;
                newTouchInfo.m_clicks = m_isDoubleClick ? 2 : 1;
                FCView lastFocusedView = m_native.getFocusedView();
                m_native.onMouseDown(newTouchInfo);
                FCView focusedView = m_native.getFocusedView();
                if (focusedView is FCTextBox)
                {
                    showTextBox(focusedView, lastFocusedView);
                }
                else
                {
                    hideTextBox(focusedView);
                }
            }
        }

        public TextBox m_textBox;

        /// <summary>
        /// 虚拟输入框
        /// </summary>
        public FCView m_editingTextBox;


        /// <summary>
        /// 显示文本框
        /// </summary>
        /// <param name="view"></param>
        /// <param name="lastView"></param>
        public void showTextBox(FCView view, FCView lastView)
        {
            if (m_textBox != null && m_editingTextBox != null)
            {
                if (lastView != null && lastView != view && lastView is FCTextBox)
                {
                    lastView.setText(m_textBox.Text);
                    lastView.invalidate();
                }
            }
            m_editingTextBox = view;
            int clX = m_native.clientX(view);
            int clY = m_native.clientY(view);
            double scaleFactorX = 0, scaleFactorY = 0;
            m_native.getPaint().getScaleFactor(ref scaleFactorX, ref scaleFactorY);
            int width = (int)(m_native.getSize().cx * scaleFactorX);
            int height = (int)(m_native.getSize().cy * scaleFactorY);
            double right = width - clX * scaleFactorX - view.getWidth() * scaleFactorX;
            double bottom = height - clY * scaleFactorY - view.getHeight() * scaleFactorY;
            m_textBox.Margin = new Thickness(clX * scaleFactorX, clY * scaleFactorY, right, bottom);
            m_textBox.Text = view.getText();
            UWPPaint uwpPaint = m_native.getPaint() as UWPPaint;
            m_textBox.Background = new SolidColorBrush(uwpPaint.getUWPColor(view.getBackColor()));
            m_textBox.Foreground = new SolidColorBrush(uwpPaint.getUWPColor(view.getTextColor()));
            FCFont font = view.getFont();
            String fontFamily = font.m_fontFamily;
            if (fontFamily == "Default")
            {
                fontFamily = MyColor.getSystemFont();
            }
            float fontSize = font.m_fontSize;
            if (scaleFactorX != 1 || scaleFactorY != 1)
            {
                fontSize = (float)(Math.Min(scaleFactorX, scaleFactorY) * fontSize);
            }
            m_textBox.FontSize = fontSize;
            m_textBox.FontFamily = new FontFamily(fontFamily); ;
            m_textBox.Visibility = Visibility.Visible;
            m_textBox.Focus(FocusState.Pointer);
        }

        /// <summary>
        /// 隐藏文本框
        /// </summary>
        /// <param name="view"></param>
        public void hideTextBox(FCView view)
        {
            if (m_textBox != null && m_editingTextBox != null)
            {
                m_editingTextBox.setText(m_textBox.Text);
                m_textBox.Visibility = Visibility.Collapsed;
                m_editingTextBox = null;
                m_native.invalidate();
            }
        }


        /// <summary>
        /// 加载视图
        /// </summary>
        public void onLoad()
        {
            m_textBox = new TextBox();
            m_textBox.Visibility = Visibility.Collapsed;
            gridMain.Children.Add(m_textBox);
            m_xml = new MainFrame();
            m_xml.createNative();
            m_native = m_xml.getNative();
            m_native.setPaint(new UWPPaint());
            m_host = new UWPHost();
            //m_host.m_view = this;
            m_native.setHost(m_host);
            m_host.setNative(m_native);
            m_native.setSize(new FCSize(800, 700));
            string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
            <html xmlns=""facecat"">
            <body>
                <div bordercolor=""none"" name=""divInner"" dock=""fill"">
                    <div type=""tab"" dock=""fill"" selectedindex=""0"" backcolor=""none"" bordercolor=""none""
                    name=""tabFunc"">
                        <div type=""tabpage"" text=""主界面"" name=""divMain"" backcolor=""none"" bordercolor=""none"">
                        <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                                size=""610,610"" candragsplitter=""true"" splitterposition=""610,1"" name=""divCodingRight2"">
                                <div type=""custom"" cid=""securitylist"" name=""gridStocks"" headerheight=""30"" dock=""fill""
                                    gridlinecolor=""none"" bordercolor=""none"" showvscrollbar=""true"" showhscrollbar=""true""
                                    allowpreviewsevent=""true"" allowdragscroll=""true"">
                                    <tr>
                                        <th name=""colP0"" text=""序"" width=""40"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                        <th name=""colP1"" text=""代码"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                        <th name=""colP2"" text=""名称"" width=""70"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" />
                                        <th name=""colP3"" text=""现价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP4"" text=""涨幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP5"" text=""涨跌"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP9"" text=""总量"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP10"" text=""总额"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP11"" text=""量比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP12"" text=""PE动"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP13"" text=""振幅"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP14"" text=""换手率"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP15"" text=""总市值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP16"" text=""流值"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP17"" text=""行业分类板块"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""center""/>
                                        <th name=""colP18"" text=""涨停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP19"" text=""跌停价"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP20"" text=""金比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP21"" text=""涨跌比"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP22"" text=""涨速"" width=""60"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP23"" text=""净资产收益率"" width=""100"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                        <th name=""colP24"" text=""自设指标"" width=""80"" allowdrag=""true"" allowresize=""true"" backcolor=""-200000000193""
                                            bordercolor=""-200000000108"" textcolor=""-200000000100"" cellalign=""right""/>
                                    </tr>
                                </div>
                                <div type=""splitlayout"" layoutstyle=""toptobottom"" bordercolor=""none"" dock=""fill""
                                    size=""400,400"" candragsplitter=""true"" splitterposition=""400,1"">
                                    <div type=""splitlayout"" layoutstyle=""righttoleft"" bordercolor=""none"" dock=""fill""
                                        size=""400,400"" candragsplitter=""true"" splitterposition=""200,1"">
                                        <div type=""custom"" cid=""latestdiv"" name=""divLatest"" />
                                        <div type=""custom"" cid=""mychart"" name=""mainChart1"" cycle=""0"" />
                                    </div>
                                    <div type=""splitlayout"" layoutstyle=""lefttoright"" bordercolor=""none"" dock=""fill""
                                        size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                        <div type=""custom"" cid=""mychart"" name=""mainChart2"" cycle=""5"" />
                                        <div type=""custom"" cid=""mychart"" name=""mainChart3"" cycle=""1440"" />
                                    </div>
                                </div>
                            </div>
                            </div>
                            <div type=""tabpage"" text=""多K线"" name=""divMulti"" backcolor=""none"" bordercolor=""none"">
                                <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                    dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""133,1"">
                                    <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                            <div type=""custom"" cid=""mychart"" name=""chart1"" cycle=""1"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                            <div type=""custom"" cid=""mychart"" name=""chart2"" cycle=""5"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                        </div>
                                        <div type=""custom"" cid=""mychart"" name=""chart3"" cycle=""10"" nativerefresh=""true"" candledivpercent=""1""
                                            voldivpercent=""0"" />
                                    </div>
                                    <div type=""splitlayout"" layoutstyle=""lefttoright"" backcolor=""none"" bordercolor=""none""
                                        dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                            <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                                dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                                <div type=""custom"" cid=""mychart"" name=""chart4"" cycle=""15"" nativerefresh=""true"" candledivpercent=""1""
                                                    voldivpercent=""0"" />
                                                <div type=""custom"" cid=""mychart"" name=""chart5"" cycle=""20"" nativerefresh=""true"" candledivpercent=""1""
                                                    voldivpercent=""0"" />
                                            </div>
                                            <div type=""custom"" cid=""mychart"" name=""chart6"" cycle=""30"" nativerefresh=""true"" candledivpercent=""1""
                                                voldivpercent=""0"" />
                                        </div>
                                        <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                            dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""267,1"">
                                            <div type=""splitlayout"" layoutstyle=""toptobottom"" backcolor=""none"" bordercolor=""none""
                                                dock=""fill"" size=""400,400"" candragsplitter=""true"" splitmode=""percentsize"" splitterposition=""200,1"">
                                                <div type=""custom"" cid=""mychart"" name=""chart7"" cycle=""1440"" nativerefresh=""true""
                                                    candledivpercent=""1"" voldivpercent=""0"" />
                                                <div type=""custom"" cid=""mychart"" name=""chart8"" cycle=""10080"" nativerefresh=""true""
                                                    candledivpercent=""1"" voldivpercent=""0"" />
                                            </div>
                                            <div type=""custom"" cid=""mychart"" name=""chart9"" cycle=""43200"" nativerefresh=""true""
                                                candledivpercent=""1"" voldivpercent=""0"" />
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div type=""tabpage"" text=""输入框"" name=""divTextBox"" backcolor=""none"" bordercolor=""none"">
                                <input type=""text"" name=""TextBox"" location=""25,25"" size=""100,20""  />
                                <input type=""text"" name=""TextBox2"" location=""25,65"" size=""100,20""  />
                                <input type=""text"" name=""TextBox3"" location=""25,106"" size=""100,20""  />
                            </div>
                        </div>
                    </div>
            </body>
            </html>
            ";
            m_xml.loadXml(xml, null);
            m_native.update();
        }
    }
}
